package NewTmas.EditMode;

import NewTmas.Graphs.GraphVizGraph;
import taskmaster.auth.User;
import taskmaster.job.JobAllTasksCollection;
import taskmaster.util.LookupLists;

import javax.swing.*;
import java.awt.*;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: abhatnag
 * Date: 8/1/11
 * Time: 2:32 PM
 * To create the editing window  that displays the editing commands and job graph
 */
public class EditIntFrame extends JInternalFrame
{
    JDesktopPane dtp;

    LookupLists ll;
    JobAllTasksCollection alltc;
    GraphVizGraph graphVizGraph = null;
    User user;
    String job;
    List<EditStep> sqlCommands = new LinkedList<EditStep>();
    JList sqlcommandsList;
    DefaultListModel model = new DefaultListModel();
    JCheckBox commitCBox = new JCheckBox("Commit Changes");

    public EditIntFrame(String jobname, User u, JDesktopPane dtp, LookupLists l, int graphWidth, int graphHeight)
    {
        this.user = u;
        this.job = jobname;
        this.dtp = dtp;
        this.ll = l;


        dtp.add(this);
        toFront();
        setTitle("EDITING : " + job);
        setVisible(true);
        setSize(400, 400);
        setResizable(true);
        setMaximizable(true);
        setClosable(true);
        setIconifiable(true);

        alltc = new JobAllTasksCollection(user.getConnection(), job, "GraphViz");
        graphVizGraph = new GraphVizGraph(alltc, job, graphWidth, graphHeight);
        graphVizGraph.getGrappaPanel().addGrappaListener(new EditGrappaAdapter());

        add(graphVizGraph.jsp);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.PAGE_AXIS));
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        JLabel editActions = new JLabel("Simple Actions");
        editActions.setForeground(Color.BLUE);
        buttonPanel.add(editActions);

        JButton addEdgeBtn = new JButton("Add Dependency");
        buttonPanel.add(addEdgeBtn);

        JButton addTaskBtn = new JButton("Add Task");
        buttonPanel.add(addTaskBtn);

        JButton delEdgeBtn = new JButton("Delete Dependency");
        buttonPanel.add(delEdgeBtn);

        JButton editTaskBtn = new JButton("Edit Task");
        buttonPanel.add(editTaskBtn);

        JSeparator sep1 = new JSeparator(SwingConstants.HORIZONTAL);
        sep1.setMaximumSize(new Dimension(500, 2));
        buttonPanel.add(sep1);
        buttonPanel.add(Box.createRigidArea(new Dimension(0, 10)));

        JLabel storedProc = new JLabel("Stored Procedures");
        storedProc.setForeground(Color.BLUE);
        buttonPanel.add(storedProc);

        JButton addTaskBetweenBtn = new JButton("Add Task Between");
        addTaskBetweenBtn.setMaximumSize(new Dimension(160, 23));
        buttonPanel.add(addTaskBetweenBtn);

        JButton addTaskAfterBtn = new JButton("Add Task After");
        buttonPanel.add(addTaskAfterBtn);

        JButton splitParallelBtn = new JButton("Add Task Parallel");
        buttonPanel.add(splitParallelBtn);

        JButton splitSerialBtn = new JButton("Add Task Serial");
        buttonPanel.add(splitSerialBtn);

        JButton applyChanges = new JButton("Apply Changes");
        JButton clearChanges = new JButton("Clear");

        JButton undoBtn = new JButton("Undo");
        undoBtn.setToolTipText("Undo Last Change");

        JButton renameTaskBtn = new JButton("Rename Task");
        buttonPanel.add(renameTaskBtn);

        JButton delTaskBtn = new JButton("Remove Task");
        buttonPanel.add(delTaskBtn);

        JButton removeBelow = new JButton("Remove Subtree");
        buttonPanel.add(removeBelow);

        JSeparator sep = new JSeparator(SwingConstants.HORIZONTAL);
        sep.setMaximumSize(new Dimension(500, 2));
        buttonPanel.add(sep);
        buttonPanel.add(Box.createRigidArea(new Dimension(0, 10)));

        JLabel editjoblbl = new JLabel("Job-Level Stored Procedures");
        editjoblbl.setForeground(Color.BLUE);
        buttonPanel.add(editjoblbl);

        JButton copyBelow = new JButton("Copy Subtree into Other Job");
        buttonPanel.add(copyBelow);

        JButton copyJobBtn = new JButton("Duplicate Job as New Job");
        buttonPanel.add(copyJobBtn);

        JButton delJobBtn = new JButton("Remove Job");
        buttonPanel.add(delJobBtn);

        JSeparator sep3 = new JSeparator(SwingConstants.HORIZONTAL);
        sep3.setMaximumSize(new Dimension(500, 2));
        buttonPanel.add(sep3);
        buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));

        JLabel label = new JLabel("Commands to execute:");
        label.setForeground(Color.BLUE);
        buttonPanel.add(label);

        sqlcommandsList = new JList(sqlCommands.toArray());
        sqlcommandsList.setBackground(Color.lightGray);
        sqlcommandsList.setOpaque(true);
        JScrollPane jpane = new JScrollPane(sqlcommandsList);

        buttonPanel.add(jpane);

        buttonPanel.setPreferredSize(new Dimension(250, getHeight()));

        buttonPanel.add(Box.createRigidArea(new Dimension(0, 10)));

        buttonPanel.add(commitCBox);


        buttonPanel.add(Box.createRigidArea(new Dimension(0, 5)));
        buttonPanel.add(applyChanges);

        buttonPanel.add(Box.createRigidArea(new Dimension(0, 5)));
        buttonPanel.add(clearChanges);

        buttonPanel.add(Box.createRigidArea(new Dimension(0, 5)));
        buttonPanel.add(undoBtn);

        getContentPane().add(buttonPanel, BorderLayout.WEST);

        applyChanges.setMaximumSize(new Dimension(140, 10));
        clearChanges.setMaximumSize(new Dimension(140, 10));
        undoBtn.setMaximumSize(new Dimension(140, 10));

        addEdgeBtn.setBorderPainted(false);
        addEdgeBtn.setContentAreaFilled(false);
        addTaskAfterBtn.setBorderPainted(false);

        addTaskBetweenBtn.setBorderPainted(false);
        delEdgeBtn.setBorderPainted(false);
        delTaskBtn.setBorderPainted(false);
        renameTaskBtn.setBorderPainted(false);
        addTaskAfterBtn.setContentAreaFilled(false);

        addTaskBetweenBtn.setContentAreaFilled(false);
        delEdgeBtn.setContentAreaFilled(false);
        delTaskBtn.setContentAreaFilled(false);
        renameTaskBtn.setContentAreaFilled(false);
        addTaskBtn.setContentAreaFilled(false);
        addTaskBtn.setBorderPainted(false);
        splitParallelBtn.setContentAreaFilled(false);
        splitParallelBtn.setBorderPainted(false);
        splitSerialBtn.setContentAreaFilled(false);
        splitSerialBtn.setBorderPainted(false);
        copyBelow.setContentAreaFilled(false);
        copyBelow.setBorderPainted(false);
        removeBelow.setBorderPainted(false);
        removeBelow.setContentAreaFilled(false);
        copyJobBtn.setContentAreaFilled(false);
        copyJobBtn.setBorderPainted(false);
        delJobBtn.setContentAreaFilled(false);
        delJobBtn.setBorderPainted(false);
        editTaskBtn.setBorderPainted(false);
        editTaskBtn.setContentAreaFilled(false);


        final ImageIcon addIcon = new ImageIcon(EditIntFrame.class.getResource("/Resources/Icons/add 48.png"));
        final ImageIcon delIcon = new ImageIcon(EditIntFrame.class.getResource("/Resources/Icons/delete.gif"));
        final ImageIcon renameIcon = new ImageIcon(EditIntFrame.class.getResource("/Resources/Icons/Edit.png"));
        final ImageIcon copyIcon = new ImageIcon(EditIntFrame.class.getResource("/Resources/Icons/copy.gif"));
        final ImageIcon undoIcon = new ImageIcon(EditIntFrame.class.getResource("/Resources/Icons/undo.gif"));
        undoBtn.setIcon(undoIcon);
        delTaskBtn.setIcon(delIcon);
        addEdgeBtn.setIcon(addIcon);
        addTaskAfterBtn.setIcon(addIcon);
        addTaskBetweenBtn.setIcon(addIcon);
        addTaskBtn.setIcon(addIcon);
        delEdgeBtn.setIcon(delIcon);
        renameTaskBtn.setIcon(renameIcon);
        clearChanges.setIcon(delIcon);
        applyChanges.setIcon(new ImageIcon(EditIntFrame.class.getResource("/Resources/Icons/Apply.png")));
        splitParallelBtn.setIcon(addIcon);
        splitSerialBtn.setIcon(addIcon);
        copyBelow.setIcon(copyIcon);
        removeBelow.setIcon(delIcon);
        copyJobBtn.setIcon(copyIcon);
        delJobBtn.setIcon(delIcon);
        editTaskBtn.setIcon(renameIcon);


        addEdgeBtn.addActionListener(new CreateDependencyListener(job, graphVizGraph, user, this));
        delEdgeBtn.addActionListener(new DeleteEdgeListener(job, graphVizGraph, user, this));
        addTaskBtn.addActionListener(new AddTaskListener(job, ll, alltc, graphVizGraph, user, this));
        editTaskBtn.addActionListener(new EditTaskListener(job, graphVizGraph, user, this, ll, alltc));

        addTaskAfterBtn.addActionListener(new AddTaskAfterListener(jobname, graphVizGraph, user.getConnection(), this));
        delTaskBtn.addActionListener(new DeleteTaskListener(jobname, graphVizGraph, user.getConnection(), this));
        addTaskBetweenBtn.addActionListener(new AddTaskBetweenListener(jobname, graphVizGraph, user.getConnection(), this));
        renameTaskBtn.addActionListener(new RenameTaskListener(jobname, graphVizGraph, user.getConnection(), this));
        splitParallelBtn.addActionListener(new SplitParallelListener(jobname, graphVizGraph, user.getConnection(), this));
        splitSerialBtn.addActionListener(new SplitSerialListener(jobname, graphVizGraph, user.getConnection(), this));
        copyBelow.addActionListener(new CopyBelowListener(jobname, graphVizGraph, user.getConnection(), this, ll));
        removeBelow.addActionListener(new RemoveBelowListener(jobname, graphVizGraph, user.getConnection(), this));

        copyJobBtn.addActionListener(new CopyJobListener(jobname, graphVizGraph, user.getConnection(), this, ll));
        delJobBtn.addActionListener(new DeleteJobListener(jobname, graphVizGraph, user.getConnection(), this));

        applyChanges.addActionListener(new ApplyGraphChangesListener(sqlCommands, this));
        clearChanges.addActionListener(new ClearChangesListener(this, user.getConnection(), graphVizGraph, job));
        undoBtn.addActionListener(new UndoLastChangeListener(this));

    }


    public void refreshAndRedrawGraph(boolean commit)
    {
        JobAllTasksCollection newTC = new JobAllTasksCollection(user.getConnection(), job, "GraphViz");
        graphVizGraph.redrawGraph(newTC.dependency_task, newTC.task_type, newTC.defaultNames);
        if (commit)
        {
            alltc = newTC;
        }
    }
}
